package net.anzhuo.controller.db;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.CompanyNameUtils;
import net.anzhuo.common.utils.DateUtil;

import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsBranch;

import net.anzhuo.entity.GsEnquityPenetration;
import net.anzhuo.entity.GsGudong;

import net.anzhuo.entity.GsOuttouzi;

import net.anzhuo.entity.GsQuery;

import net.anzhuo.entity.db.spiderman.DetailList;
import net.anzhuo.entity.db.spiderman.Gudong;
import net.anzhuo.entity.db.spiderman.JsonRootBean;
import net.anzhuo.entity.db.spiderman.Kg;
import net.anzhuo.entity.db.spiderman.Kzr;
import net.anzhuo.entity.db.spiderman.SyrNames;
import net.anzhuo.entity.db.spiderman.Oper;
import net.anzhuo.entity.db.spiderman.Syr;
import net.anzhuo.entity.db.spiderman.Touzi;
import net.anzhuo.entity.db.spiderman.kgNames;
import net.anzhuo.entity.db.syncdata.PairValue;
import net.anzhuo.entity.db.syncdata.QueryKeyNoResult;
import net.anzhuo.entity.db.syncdata.UpdateKeyNoResult;
import net.anzhuo.entity.equitypenetration.EnquityPenetrationResult;
import net.anzhuo.entity.equitypenetration.EnquityResult;
import net.anzhuo.entity.equitypenetration.KeyNoListResult;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsBranchService;

import net.anzhuo.service.GsEnquityPenetrationService;
import net.anzhuo.service.GsGudongService;

import net.anzhuo.service.GsOuttouziService;

import net.anzhuo.service.GsQueryService;

@RestController
@Slf4j
public class EquityPenetrationController {

	
	
	@Autowired
	private TransportClient client;
	
	
	@Autowired
	private GsEnquityPenetrationService gsEnquityPenetrationService;
	
	@Autowired
	private GsBaseService gsBaseService;
	
	@Autowired
	private GsBranchService gsBranchService;
	
	@Autowired
	private GsGudongService gsGudongService;
	
	@Autowired
	private GsQueryService gsQueryService;
	
	@Autowired
	private GsOuttouziService gsOutouziService;
	
	public List<DetailList> getDetailListRep(List<DetailList> list,Gudong gudong)
	{
	
		for(int i=0;i<list.size();i++)
		{
			DetailList g=list.get(i);
			if(g.DetailCount>0)
			{
				g.DetailList=this.getDetailListRep(g.DetailList,gudong);
			}
			
			if(g.KeyNo.substring(0,1).equals("p"))
			{
				List<GsBase> gsBaselist=gsBaseService.getByNameList(gudong.Name);
				
				GsBase gsBase=new GsBase(); 
		      
				for(int x=0;x<gsBaselist.size();x++)
				{
					if(x==0)
					{
						gsBase=gsBaselist.get(x);
					}else
					{
						gsBaseService.delete(gsBaselist.get(x).getId());
					}
				}
				if(gsBase!=null)
				{
				List<GsGudong> listGudong=gsGudongService.getByMd5(gsBase.getMd5());
				for(GsGudong g1:listGudong)
				{
					
					if(g1.getShareholder()!=null)
					{
						if(g1.getShareholder().trim().equals(g.Name))
						{
							g1.setMd5(g.KeyNo);
							if(g1.getCapitaltype().equals("自然人股东"))
							{
								g1.setCapitaltype("1");
							}else if(g1.getCapitaltype().equals("企业法人"))
							{
								g1.setCapitaltype("2");
							}else if(g1.getCapitaltype().equals("法人股东"))
							{
								g1.setCapitaltype("3");
							}else if(g1.getCapitaltype().equals("事业法人"))
							{
								g1.setCapitaltype("4");
							}
							gsGudongService.update(g1);							
						}
					}
				}
				}
			}else
			{
				//如果企业不存在 启动爬虫系统
				
				List<GsBase> gEnglishBaseList=gsBaseService.getByNameList(CompanyNameUtils.getEnglishBraces(g.Name));
				List<GsBase> gChineseBaseList=gsBaseService.getByNameList(CompanyNameUtils.getChineseBraces(g.Name));
				if(gEnglishBaseList.size()==0&&gChineseBaseList.size()==0)
				{
				GsQuery gsQuery=new GsQuery();
				gsQuery.setKeyNo(g.KeyNo);
				gsQuery.setName(CompanyNameUtils.getEnglishBraces(g.Name));
				gsQuery.setStatus("0");
				gsQuery.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
				List<GsQuery> listQuery=gsQueryService.getBycoMd5(gsQuery);
				if(listQuery.size()==0)
				{
				gsQueryService.save(gsQuery);
				}else
				{
			    gsQueryService.update(gsQuery);
				}
				}
				List<GsBase> gsBaselist=gsBaseService.getByNameList(gudong.Name);
				GsBase gsBase=new GsBase(); 
				 
				for(int x=0;x<gsBaselist.size();x++)
				{
					if(x==0)
					{
						gsBase=gsBaselist.get(x);
					}else
					{
						gsBaseService.delete(gsBaselist.get(x).getId());
					}
				}
				if(gsBase!=null)
				{
					List<GsBranch> listBranch=gsBranchService.getByMd5(gudong.Name);
					for(GsBranch g2:listBranch)
					{
						if(g2.getEnterpriseName()!=null)
						{
						if(g.Name.equals(g2.getEnterpriseName().trim()))
						{ 
							g.KeyNo=g2.getMd5();
						}
						}
					}
				}
			}
			list.set(i, g);
		}
		return list;
	}
	
	
	public List<DetailList> getDetailListTouZiRep(List<DetailList> list,Touzi touzi)
	{
	
		
		for(int i=0;i<list.size();i++)
		{
			DetailList g=list.get(i);
			if(!g.KeyNo.substring(0,1).equals("p"))
			{
			List<GsBase> gEnglishBaseList=gsBaseService.getByNameList(CompanyNameUtils.getEnglishBraces(g.Name));
			List<GsBase> gChineseBaseList=gsBaseService.getByNameList(CompanyNameUtils.getChineseBraces(g.Name));
			if(gEnglishBaseList.size()==0&&gChineseBaseList.size()==0)
			{
			GsQuery gsQuery=new GsQuery();
			gsQuery.setKeyNo(g.KeyNo);
			gsQuery.setName(g.Name);
			gsQuery.setStatus("0");
			gsQuery.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
			List<GsQuery> listQuery=gsQueryService.getBycoMd5(gsQuery);
			if(listQuery.size()==0)
			{
			gsQueryService.save(gsQuery);
			}else
			{
		    gsQueryService.update(gsQuery);
			}
			}else//exists
			{
				if(gEnglishBaseList.size()>0)
				{
					for(GsBase cc:gEnglishBaseList)
					{
						g.KeyNo=cc.getMd5();
					}
				}else {
					for(GsBase cc:gChineseBaseList)
					{
						g.KeyNo=cc.getMd5();
					}
				}
			}
			}
			if(g.DetailCount>0)
			{
				g.DetailList=this.getDetailListTouZiRep(g.DetailList,touzi);
			}
			List<GsBase> gsBaselist=gsBaseService.getByNameList(touzi.Name);
			GsBase gsBase=new GsBase(); 
			for(int x=0;x<gsBaselist.size();x++)
			{
				if(x==0)
				{
					gsBase=gsBaselist.get(x);
				}else
				{
					gsBaseService.delete(gsBaselist.get(x).getId());
				}
			}

		 if(gsBase!=null)
		 {
		 GsOuttouzi go=new GsOuttouzi();
		 go.setCoMd5(gsBase.getMd5());
		 go.setEnterpriseName(g.Name);
		 List<GsOuttouzi> listOut=gsOutouziService.getListByName(go);
		  for(GsOuttouzi g1:listOut)
		  {
			 g.KeyNo=g1.getMd5();
		  }
		 }
			
		list.set(i, g);
		}
		return list;
	}
	
   public List<kgNames> getNames(List<kgNames> list)
   {
	   ArrayList<kgNames> lista=new ArrayList<kgNames>();
	   for(kgNames g:list)
	   {
			if(!g.KeyNo.substring(0,1).equals("p"))
			{
				List<GsBase> gEnglishBaseList=gsBaseService.getByNameList(CompanyNameUtils.getEnglishBraces(g.Name));
				List<GsBase> gChineseBaseList=gsBaseService.getByNameList(CompanyNameUtils.getChineseBraces(g.Name));
				if(gEnglishBaseList.size()==0&&gChineseBaseList.size()==0)
				{
				    GsQuery gsQuery=new GsQuery();
				    gsQuery.setKeyNo(g.KeyNo);
				    gsQuery.setName(g.Name);
				    gsQuery.setStatus("0");
				    gsQuery.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
				    List<GsQuery> listQuery=gsQueryService.getList(gsQuery);
				if(listQuery.size()==0)
				{
				   gsQueryService.save(gsQuery);
				}else
				{
			       gsQueryService.update(gsQuery);
				}
				}
				
			}
		   List<GsBase> gsBaselist=gsBaseService.getByNameList(g.Name);
			GsBase gs=new GsBase(); 
			for(int x=0;x<gsBaselist.size();x++)
			{
				if(x==0)
				{
					gs=gsBaselist.get(x);
				}else
				{
					gsBaseService.delete(gsBaselist.get(x).getId());
				}
			}

		   if(gs!=null)
		   {
			   g.KeyNo=gs.getMd5();
			 
			   GsGudong gss=new GsGudong();
			   gss.setCoMd5(gs.getMd5());
			   gss.setShareholder(g.Oper.Name);
			   List<GsGudong> listOper= gsGudongService.getByName(gss);
			   if(listOper.size()>0)
			   {
				   GsGudong gsGudong=  listOper.get(0);
				   
				   Oper op=g.Oper;
				   
				   op.KeyNo=gsGudong.getMd5();
				   g.Oper=op;
			   }
		   }
		  
		   g.ImageUrl=g.ImageUrl.replaceAll("/", "\\/");
		   lista.add(g);
	   }
	   
	   return lista;
   }
   
   
   public List<SyrNames> getSYRNames(List<SyrNames> list,Syr syr)
   {
	   ArrayList<SyrNames> lista=new ArrayList<SyrNames>();
	   for(SyrNames g:list)
	   {
		   if(!g.KeyNo.substring(0,1).equals("p"))
			{
				List<GsBase> gEnglishBaseList=gsBaseService.getByNameList(CompanyNameUtils.getEnglishBraces(g.Name));
				List<GsBase> gChineseBaseList=gsBaseService.getByNameList(CompanyNameUtils.getChineseBraces(g.Name));
				if(gEnglishBaseList.size()==0&&gChineseBaseList.size()==0)
				{
				GsQuery gsQuery=new GsQuery();
				gsQuery.setKeyNo(g.KeyNo);
				gsQuery.setName(g.Name);
				gsQuery.setStatus("0");
				gsQuery.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
				List<GsQuery> listQuery=gsQueryService.getBycoMd5(gsQuery);
				if(listQuery.size()==0)
				{
				gsQueryService.save(gsQuery);
				}else
				{
			    gsQueryService.update(gsQuery);
				}
				}
				
			}
			   GsGudong gss=new GsGudong();
			   gss.setShareholder(g.Name);
			   gss.setCoMd5(syr.KeyNo);
			   List<GsGudong> listOper= gsGudongService.getByName(gss);
			   if(listOper.size()>0)
			   {
				   GsGudong gsGudong=  listOper.get(0);
			       gsGudong.setMd5(g.KeyNo);	   			
			   }		   
		   lista.add(g);
	   }
	   
	   return lista;
   }
	
	@RequestMapping("/EnquityKeyNoList")
	public QueryKeyNoResult EnquityKeyNoList(String status) {
		log.info("===>EnquityKeyNoList="+status);
		long startTime=System.currentTimeMillis();
		QueryKeyNoResult qknr=new QueryKeyNoResult();		
		try {
	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsEnquityPenetration gp=new GsEnquityPenetration();
			gp.setStatus(status);
		   List<GsEnquityPenetration> list=gsEnquityPenetrationService.getList(gp);
		  
		   List<PairValue> keyNoList=new ArrayList<PairValue>();
		   PairValue pv=new PairValue();
		   for(GsEnquityPenetration g:list)
		   {
			  
			   pv=new PairValue();
			   pv.setKeyNo(g.getKeyNo());
			   if(gsBaseService.getByMd5(g.getKeyNo())!=null)
					   {
				   if(gsBaseService.getByMd5(g.getKeyNo()).getName()!=null&&!gsBaseService.getByMd5(g.getKeyNo()).getName().equals(""))
			       {
					   pv.setName(gsBaseService.getByMd5(g.getKeyNo()).getName());
					   pv.setCreditCode(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode());
					   log.info("Name not null="+g.getKeyNo()+":"+pv.getName());
			       }else
					   {
			    	   if(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode()!=null&&!gsBaseService.getByMd5(g.getKeyNo()).getCreditCode().equals(""))
			    	   {
			    		  // pv.setName(gsBaseService.getByMd5(g.getKeyNo()).getCreditCode());
			    		  // log.info("Name is null and credit is not null="+pv.getName());
			    	   }else
			    	   {
			    		   continue;
			    	   }
			    	   }
					   }	   
			   keyNoList.add(pv); 
		   }
		   qknr.setCount(keyNoList.size());
		   log.info("size="+keyNoList.size());
		   qknr.setKeyNoList(keyNoList);
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}	
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" EnquityKeyNoList 执行时间："+excTime+"s");
		return qknr;
	}
	@RequestMapping("/UpdateEnquityKeyNo")
	public UpdateKeyNoResult KeyNoList(String keyNo,String data,String status) {
		log.info("===>UpdateEnquityKeyNo="+keyNo);
		long startTime=System.currentTimeMillis();
		UpdateKeyNoResult qknr=new UpdateKeyNoResult();
		
		try {
	
			qknr.setStatus("200");
			qknr.setMessage("查询成功");
			GsEnquityPenetration gp=new GsEnquityPenetration();
			gp.setKeyNo(keyNo);
			gp.setStatus(status);
			gp.setData(data);
			Date date= new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str = sdf.format(date);
			
			gp.setUpdateTime(str);
			gsEnquityPenetrationService.update(gp);
		 
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}
		
		
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" UpdateEnquityKeyNo 执行时间："+excTime+"s");
		return qknr;
	}
	@RequestMapping("/EquityPenetration")
	public EnquityPenetrationResult EquityPenetration(String keyword) {
		log.info("===>EquityPenetration="+keyword);
		EnquityPenetrationResult ep=new EnquityPenetrationResult();
		if(keyword==null||keyword.equals(""))
		{
			return ep;
		}
		QueryBuilder query = QueryBuilders.boolQuery()
				.must(QueryBuilders.boolQuery()
					.must(QueryBuilders.matchPhraseQuery("md5",keyword)));
		ArrayList<KeyNoListResult> list=new ArrayList<KeyNoListResult>();
		KeyNoListResult knr=new KeyNoListResult();
		SearchResponse response = client.prepareSearch("gs").setTypes("gs_base").setQuery(query).addSort("weight", SortOrder.DESC).addSort("_score", SortOrder.DESC).setFrom((Integer.parseInt("1")-1)*200).setSize(200).execute().actionGet();
		if (response.getHits().getTotalHits()>0) {
			{
				for (SearchHit searchHit : response.getHits()) {	
					knr=new KeyNoListResult();
					knr.setKeyNo((String)searchHit.getSource().get("md5"));
					knr.setName((String)searchHit.getSource().get("name"));
					list.add(knr);
					log.info("name="+(String)searchHit.getSource().get("name"));
					GsEnquityPenetration gs=new GsEnquityPenetration();
					gs.setKeyNo(knr.getKeyNo());
					gs.setLegal(knr.getLegal());
					gs.setName(knr.getName());
					gs.setStatus("0");
					gs.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
					List<GsEnquityPenetration> li=gsEnquityPenetrationService.getBycoMd5(knr.getKeyNo());
					if(li.size()==0)
					{
					gsEnquityPenetrationService.save(gs);
					}else
					{
						gs.setId(li.get(0).getId());
						if(li.get(0).getData().length()>200)
						{
						  gs.setStatus("1");
					      gsEnquityPenetrationService.update(gs);
						}else
						{
						gsEnquityPenetrationService.update(gs);
						}
					}
				}
			}
		}
		
		List<GsEnquityPenetration> listResult=gsEnquityPenetrationService.getBycoMd5(keyword);
		GsEnquityPenetration gp=new GsEnquityPenetration();
		if(listResult.size()>0)
		{
		
			gp=listResult.get(0);
			while(gp.getStatus().equals("0"))
			{
				listResult=gsEnquityPenetrationService.getBycoMd5(keyword);
				if(listResult.size()>0)
				{
					gp=listResult.get(0);
					
				}
			}
		}
		String data=gp.getData();

		ep.setData(data);
		long startTime=System.currentTimeMillis();
		long endTime=System.currentTimeMillis();
		float excTime=(float)(endTime-startTime)/1000;
		log.info(" EquityPenetration 执行时间："+excTime+"s");
		return ep;
	}
	
	@RequestMapping("/EquityPenetrationLocal")
	public EnquityPenetrationResult EquityPenetrationLocal(String keyword) {
		log.info("===>EquityPenetrationLocal="+keyword);
		EnquityPenetrationResult ep=new EnquityPenetrationResult();
		if(keyword==null||keyword.equals(""))
		{
			return ep;
		}
		QueryBuilder query = QueryBuilders.boolQuery()
				.must(QueryBuilders.boolQuery()
					.must(QueryBuilders.matchPhraseQuery("md5",keyword)));
		ArrayList<KeyNoListResult> list=new ArrayList<KeyNoListResult>();
		KeyNoListResult knr=new KeyNoListResult();
		SearchResponse response = client.prepareSearch("gs").setTypes("gs_base").setQuery(query).addSort("weight", SortOrder.DESC).addSort("_score", SortOrder.DESC).setFrom((Integer.parseInt("1")-1)*200).setSize(200).execute().actionGet();
		if (response.getHits().getTotalHits()>0) {
			{
				for (SearchHit searchHit : response.getHits()) {	
					knr=new KeyNoListResult();
					knr.setKeyNo((String)searchHit.getSource().get("md5"));
					knr.setName((String)searchHit.getSource().get("name"));
					list.add(knr);
					log.info("name="+(String)searchHit.getSource().get("name"));
					GsEnquityPenetration gs=new GsEnquityPenetration();
					gs.setKeyNo(knr.getKeyNo());
					gs.setLegal(knr.getLegal());
					gs.setName(knr.getName());
					gs.setStatus("0");
					gs.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
					List<GsEnquityPenetration> li=gsEnquityPenetrationService.getBycoMd5(knr.getKeyNo());
					if(li.size()==0)
					{
					gsEnquityPenetrationService.save(gs);
					}else
					{
						gs.setId(li.get(0).getId());
						if(li.get(0).getData().length()>200)
						{
						gs.setStatus("1");
					    gsEnquityPenetrationService.update(gs);
						}else
						{
						gsEnquityPenetrationService.update(gs);
						}
					}
				}
			}
		}
		
		List<GsEnquityPenetration> listResult=gsEnquityPenetrationService.getBycoMd5(keyword);
		GsEnquityPenetration gp=new GsEnquityPenetration();
		if(listResult.size()>0)
		{
		
			gp=listResult.get(0);
			while(gp.getStatus().equals("0"))
			{
				listResult=gsEnquityPenetrationService.getBycoMd5(keyword);
				if(listResult.size()>0)
				{
					gp=listResult.get(0);
					
				}
			}
		}
		//keyNo replace
		String data=gp.getData();
		JsonRootBean ep1=	JSON.parseObject(data, JsonRootBean.class);
		//GuDong
		Gudong gu=null;
		if(ep1!=null)
		{ 
			gu=ep1.getGudong();
		
		
		
		List<DetailList> detailList=gu.DetailList;
		gu.DetailList=getDetailListRep(detailList,gu);

		
		Kg kg=ep1.getKg();
		if(kg!=null)
		{
        List<kgNames> NamesList=kg.Names;
        NamesList=getNames(NamesList);
        kg.Names=NamesList;
		}
        ep1.setGudong(gu);
        ep1.setKg(kg);
        
        Kzr kzr=ep1.getKzr();
        if(kzr!=null)
        {
    	List<GsBase> gsBaselist=gsBaseService.getByNameList((kzr.CompanyName));
    	GsBase gsBase=new GsBase(); 
		for(int x=0;x<gsBaselist.size();x++)
		{
			if(x==0)
			{
				gsBase=gsBaselist.get(x);
			}else
			{
				gsBaseService.delete(gsBaselist.get(x).getId());
			}
		}

        if(gsBase!=null)
        {
        	kzr.KeyNo=gsBase.getMd5();
        }
        ep1.setKzr(kzr);
        }
        Syr syr=ep1.getSyr();
        if(syr!=null)
        {
        List<GsBase>  gsBaselist=gsBaseService.getByNameList(syr.CompanyName);
        GsBase gsBase=new GsBase(); 
		for(int x=0;x<gsBaselist.size();x++)
		{
			if(x==0)
			{
				gsBase=gsBaselist.get(x);
			}else
			{
				gsBaseService.delete(gsBaselist.get(x).getId());
			}
		}
   
        if(gsBase!=null)
        {
        syr.KeyNo=gsBase.getMd5();
        }
        
        this.getSYRNames(syr.Names, syr);
        ep1.setSyr(syr);
        }
        Touzi touzi=ep1.getTouzi();
        if(touzi!=null) {
        List<DetailList>      touziDetailList=touzi.DetailList;
        touziDetailList=this.getDetailListTouZiRep(touziDetailList, touzi);
        touzi.DetailList=touziDetailList;
        ep1.setTouzi(touzi);
        }
		}
        String dataLocal=JSON.toJSONString(ep1);
		ep.setData(dataLocal);
		long startTime=System.currentTimeMillis();
		long endTime=System.currentTimeMillis();
		float excTime=(float)(endTime-startTime)/1000;
		log.info(" EquityPenetrationLocal 执行时间："+excTime+"s");
		return ep;
	}
	
	//result
	@RequestMapping("/EquityPenetrationSearchResult")
	public EnquityResult EquityPenetrationSearchResult(String keyword) {
		log.info("===>EquityPenetrationSearchResult="+keyword);
		EnquityResult ep=new EnquityResult();
		SearchResponse response=null;
		if (keyword.length()>4)
		{
		QueryBuilder query = QueryBuilders.boolQuery()
				.must(QueryBuilders.boolQuery()
					.must(QueryBuilders.matchPhraseQuery("name",keyword)));
		
		 response = client.prepareSearch("gs").setTypes("gs_base").setQuery(query).addSort("weight", SortOrder.DESC).addSort("_score", SortOrder.DESC).setFrom((Integer.parseInt("1")-1)*200).setSize(200).execute().actionGet();
		}else
		{
			QueryBuilder query = QueryBuilders.boolQuery()
					.must(QueryBuilders.boolQuery()
						.must(QueryBuilders.matchPhraseQuery("legal",keyword)));
			 response = client.prepareSearch("gs").setTypes("gs_base").setQuery(query).addSort("weight", SortOrder.DESC).addSort("_score", SortOrder.DESC).setFrom((Integer.parseInt("1")-1)*200).setSize(200).execute().actionGet();
		
		}
		List<net.anzhuo.entity.equitypenetration.Result> list=new ArrayList<net.anzhuo.entity.equitypenetration.Result>();
		if (response.getHits().getTotalHits()>0) {
			{
				for (SearchHit searchHit : response.getHits()) {	
					net.anzhuo.entity.equitypenetration.Result r=new net.anzhuo.entity.equitypenetration.Result();
					r.setKeyNo((String)searchHit.getSource().get("md5"));
					r.setName((String)searchHit.getSource().get("name"));
				    r.setLegal((String)searchHit.getSource().get("legal"));
					r.setProvince(((String)searchHit.getSource().get("province")));
					GsEnquityPenetration gs=new GsEnquityPenetration();
					gs.setKeyNo(r.getKeyNo());
					gs.setLegal(r.getLegal());
					gs.setName(r.getName());
					gs.setStatus("0");
					gs.setUpdateTime(DateUtil.getDate("yyyy-MM-dd"));
					List<GsEnquityPenetration> li=gsEnquityPenetrationService.getBycoMd5(r.getKeyNo());
					if(li.size()==0)
					{
					gsEnquityPenetrationService.save(gs);
					}else
					{
					gsEnquityPenetrationService.update(gs);
					}
					list.add(r);
				}
			}
		}
		ep.setResult(list);
		long startTime=System.currentTimeMillis();
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" EquityPenetrationSearchResult 执行时间："+excTime+"s");
		return ep;
	}
}
